<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="是否结算" prop="isPaid">
        <el-select v-model="queryParams.isPaid" placeholder="请选择是否结算" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_number_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bussiness:rent:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="rentList" @selection-change="handleSelectionChange">
      <el-table-column label="租赁类型" align="center" prop="rentType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.rent_type" :value="scope.row.rentType"/>
        </template>
      </el-table-column>
      <el-table-column label="雪具名称" align="center" show-overflow-tooltip prop="itemName" :formatter="itemNameFormatter"/>
      <el-table-column label="雪具编码" min-width="150" show-overflow-tooltip align="center" prop="itemCode" :formatter="itemCodeFormatter"/>
      <el-table-column label="租赁数量" align="center" prop="rentQuantity" />
      <el-table-column label="租赁人" align="center" prop="rentUserName" />
      <el-table-column label="租赁开始时间" align="center" prop="rentStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rentStartTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="租赁结束时间" align="center" prop="rentEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rentEndTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="租金总额（元）" align="center" prop="rentMoney" />
      <el-table-column label="是否结算" align="center" prop="isPaid">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_number_yes_no" :value="scope.row.isPaid"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_data_status" :value="scope.row.status" />
        </template> 
      </el-table-column>
      
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listRent, getRent, delRent, addRent, updateRent } from "@/api/bussiness/rent";

export default {
  name: "Rent",
  dicts: ['sys_number_yes_no', 'rent_type', 'bus_data_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 雪具租赁表格数据
      rentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isPaid: null,
        status: null,
        rentType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 表格空列 格式化 */
    itemNameFormatter(row){
      return row.itemName ? row.itemName : '-'
    },
    itemCodeFormatter(row){
      return row.itemCode ? row.itemCode : '-'
    },
    /** 查询雪具租赁列表 */
    getList() {
      this.loading = true;
      listRent(this.queryParams).then(response => {
        this.rentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rentId: null,
        billId: null,
        inventoryId: null,
        rentQuantity: null,
        rentUserName: null,
        rentStartTime: null,
        rentEndTime: null,
        rentMoney: null,
        isPaid: null,
        status: "0",
        rentType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.rentId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加雪具租赁";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rentId = row.rentId || this.ids
      getRent(rentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改雪具租赁";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.rentId != null) {
            updateRent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rentIds = row.rentId || this.ids;
      this.$modal.confirm('是否确认删除雪具租赁编号为"' + rentIds + '"的数据项？').then(function() {
        return delRent(rentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bussiness/rent/export', {
        ...this.queryParams
      }, `rent_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
